package com.mymail.myutil;

import java.util.Iterator;

import com.mymail.dao.MailboxDAO;
import com.mymail.entity.MailBox;
import com.mymail.entity.User;

public class AddMailbox {
	private final static String EXIST_ERROR="该邮箱你已经添加过了！";
	private final static String USER_ERROR="用户名或密码错误！"; 
	private final static String ADD_OK="OK";
	public static String add(String address,String password,User user){
		if(isExist(address,user))return EXIST_ERROR;
		MailBox box=new MailBox();
		box.setPassword(password);
		box.setName(address);
		box.setUser(user.getId());
		box.setServer_name(box.getName().split("@")[1]);
		MailboxDAO dao=MailBox.getDAO();
		dao.addNew(box);
		user.setMailboxs(null);
		return ADD_OK;
	}
	public static boolean isExist(String address,User user){
		Iterator<MailBox> it=user.getMailboxs().iterator();
		while(it.hasNext()){
			if(address.trim().equals(it.next().getName()))return true;
		}
		return false;
	}
}
